//////////////////////////////////////////////////////////////////////////////
// Shared resources to drive GFx
//////////////////////////////////////////////////////////////////////////////

#ifndef _SHARED_RESOURCES_H_
#define _SHARED_RESOURCES_H_

#pragma once


#ifndef EXCLUDE_SCALEFORM_SDK


#pragma warning(push)
#pragma warning(disable : 6326) // Potential comparison of a constant with another constant
#pragma warning(disable : 6011) // Dereferencing NULL pointer
#include <GFxLoader.h>
#pragma warning(pop)

#include "ConfigScaleform.h"
#include "GAllocatorCryMem.h"
#include "GRendererXRender.h"


class GFxLoader2;
class ICrySizer;
class GSystemInitWrapper;
class MeshCacheResetThread;


class CSharedFlashPlayerResources
{
public:
	static CSharedFlashPlayerResources& GetAccess();

	static void Init();
	static void Shutdown();

public:
	GFxLoader2* GetLoader(bool getRawInterface = false);
	GRendererXRender* GetRenderer(bool getRawInterface = false);

	CryGFxMemInterface::Stats GetSysAllocStats() const;
	void GetMemoryUsage(ICrySizer* pSizer) const;

	int CreateTempMemoryArena() const;
	void DestoryTempMemoryArena() const;
	bool IsTempMemoryArenaActive() const;

	void ResetMeshCache() const;

private:
	CSharedFlashPlayerResources();
	~CSharedFlashPlayerResources();

private:
	static CSharedFlashPlayerResources* ms_pSharedFlashPlayerResources;

private:
	GSystemInitWrapper* m_pGSystemInit;
	GFxLoader2* m_pLoader;
	GRendererXRender* m_pRenderer;
	MeshCacheResetThread* m_pMeshCacheResetThread;
};


class GFxLoader2 : public GFxLoader
{
	friend class CSharedFlashPlayerResources;

public:
	// cannot configure GFC's RefCountBase via public inheritance so implement own ref counting
	void AddRef();
	void Release();
	int GetRefCount() const { return m_refCount; }

	void UpdateParserVerbosity();

	void GetMemoryUsage(ICrySizer* pSizer) const
	{
		pSizer->Add(this, sizeof(*this)); // just add object, internals are counted implicitly by provided GFxCryMemInterface / GSysAlloc
	}

private:
	GFxLoader2();
	virtual ~GFxLoader2();
	void SetupDynamicFontCache();

private:
	volatile int m_refCount;
	GFxParseControl m_parserVerbosity;
};


class MeshCacheResetThread : public CrySimpleThread<>
{
public:
	// CrySimpleThread
	virtual void Run();
	virtual void Cancel();

public:
	MeshCacheResetThread();
	~MeshCacheResetThread();

	void IssueReset();

private:
	static const char* ms_pThreadName;

private:
	volatile bool m_cancelRequestSent;
	CryEvent m_awakeThread;
};

#endif // #ifndef EXCLUDE_SCALEFORM_SDK


#endif // #ifndef _SHARED_RESOURCES_H_
